package com.xiyu.service.repository.elder.register;

import com.xiyu.service.model.elder.info.ElderInfoFetcher;
import com.xiyu.service.model.elder.register.ElderGoOutFetcher;
import com.xiyu.service.vo.elder.goOut.GoOutPageQueryInput;
import com.xiyu.service.model.elder.register.ElderGoOut;
import com.xiyu.service.model.elder.register.ElderGoOutTable;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;
import org.springframework.util.StringUtils;
import java.util.Optional;

public interface ElderGoOutRepository extends JRepository<ElderGoOut, Long> {
    ElderGoOutTable elderGoOutTable = ElderGoOutTable.$;

    default Page<ElderGoOut> pageQuery(GoOutPageQueryInput inputVO){
        return sql().createQuery(elderGoOutTable)
                .whereIf(inputVO.getEntourageType() != null, elderGoOutTable.entourageType().eq(inputVO.getEntourageType()))
                .whereIf(inputVO.getGoOutTime() != null, () -> elderGoOutTable.goOutTime().between(inputVO.getGoOutTime()[0], inputVO.getGoOutTime()[1]))
                .whereIf(StringUtils.hasText(inputVO.getEntourageName()), () -> elderGoOutTable.entourageName().like(inputVO.getEntourageName()))
                .whereIf(StringUtils.hasText(inputVO.getElderName()), () -> elderGoOutTable.elder().name().like(inputVO.getElderName()))
                .orderBy(elderGoOutTable.id().desc())
                .select(elderGoOutTable.fetch(ElderGoOutFetcher.$.allTableFields().elder(ElderInfoFetcher.$.allTableFields())))
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());

    }

    default Optional<ElderGoOut> findDetailById(Long id){
        return sql().createQuery(elderGoOutTable)
                .where(elderGoOutTable.id().eq(id))
                .select(elderGoOutTable.fetch(ElderGoOutFetcher.$.allTableFields().elder(ElderInfoFetcher.$.allTableFields())))
                .fetchOptional();
    }


}